// pages/index/psychology/assessment/assessment.js
Page({

  /**
   * Page initial data
   */
  data: {
    page: 1,
    size: 8,
    heartMark: [],
    loading: false, // 加载状态
    hasMoreData: true
  },

  //获取后端数据
  selectAll() {
    const that = this;
    wx.request({
      url: `http://localhost:8080/heartmark/findAllHeartMark?page=${that.data.page}&size=${that.data.size}`,
      success(res) {
        console.log(res);
        that.setData({
          heartMark: res.data.data.list
        })
      }
    })
  },


  bottomHearMark() {
    if (this.data.loading || !this.data.hasMoreData) return;
    this.setData({
      loading: true
    });
    wx.request({
      url: `http://localhost:8080/heartmark/findAllHeartMark?page=${this.data.page}&size=${this.data.size}`,
      success: (res) => {
        const newCourses = res.data.data.list;
        console.log(newCourses);
        if (newCourses && newCourses.length > 0) {
          if (newCourses.length < this.data.size) {
            this.setData({
              hasMoreData: false
            });
          }
          if (this.data.page === 1) {
            this.setData({
              heartMark: newCourses
            });
          } else {
            this.setData({
              heartMark: this.data.heartMark.concat(newCourses)
            });
          }
          this.setData({
            page: this.data.page + 1
          }); // 增加页码
        } else {
          this.setData({
            hasMoreData: false
          });
        }
      },
      fail: (err) => {
        console.error(err);
      },
      complete: () => {
        this.setData({
          loading: false
        });
      }
    });
  },

  //点击跳转
  goNext(e){
    const id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: `/pages/index/psychology/assessment/assessmentlist/assessmentlist?id=${id}`,
    })
  },


  /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {
    this.selectAll()
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom() {
    setTimeout(() => {
      this.bottomHearMark(); // 调用加载更多方法
    }, 1000);

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage() {

  }
})